// pages/chef/chef.js
import api from '../../utils/api.js'
let app = getApp();
import { HTTP } from '../../utils/http.js'
import { config } from '../../utils/config'
let http = new HTTP()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        sureJyqc: false, // 用户是否关注改厨师
        current: -1, // nav选中的下标
        chef: "", // 单个厨师的详情
        list: [], // 菜谱列表
        size: 6, // 每页展示的菜谱数量
        page: 1, // 当前页码
        over: false, // 菜谱数据是否全部加载
        num: 1, // 已选菜单的数量
        imgUrl: config.imgUrl,
        show: false // 更多导航是否展示
    },

    // 接口
    // 获得单个厨师详情接口
    getOneJyqc(obj) {
        http.request({
            url: api.getOneJyqc,
            data: obj,
            success: res => {
                console.log(res.data)
                this.setData({
                    chef: res.data
                })
            }
        })
    },
    // 判断用户是否关注一位厨师接口
    sureJyqc(obj) {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.sureJyqc,
                data: obj,
                success: res => {
                    console.log(res)
                    var sureJyqc = true
                    if (res.message == "该用户还没有点赞这条留言") {
                        sureJyqc = false
                    }
                    this.setData({
                        sureJyqc
                    })
                }
            })
        })
    },
    // 用户关注与取消关注一位厨师接口
    addDelJyqc(obj) {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.addDelJyqC,
                data: obj,
                success: res => {
                    wx.showToast({
                        title: res.message,
                        duration: 1500
                    })
                    var chef = this.data.chef
                    chef.attention++
                        if (res.message == "取消成功") {
                            chef.attention = chef.attention - 2
                        }
                    this.setData({
                        chef
                    })
                }
            })
        })
    },
    // 获得单个厨师所有菜谱
    getAllCookBooks(obj, order) {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.getAllCookBookS,
                data: obj,
                success: res => {
                    console.log(res.data)
                    var list = []
                    var data = res.data
                    if (order == 1) {
                        list = this.data.list
                        if (res.msg == "该厨师还没有相关菜谱") {
                            data = []
                            this.setData({
                                over: true
                            })
                        }
                    }
                    this.setData({
                        list: list.concat(data)
                    })
                }
            })
        })
    },
    // 获得单个厨师单个菜谱分类下的所有菜谱
    getCookbooksById(obj, order) {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.getCookbooksByID,
                data: obj,
                success: res => {
                    console.log(res.data)
                    var list = []
                    var data = res.data
                    if (order == 1) {
                        list = this.data.list
                        if (res.msg == "该厨师还没有相关菜谱") {
                            data = []
                            this.setData({
                                over: true
                            })
                        }
                    }
                    this.setData({
                        list: list.concat(data)
                    })
                }
            })
        })
    },


    // 用户点击关注和取消关注改厨师
    attention() {
        var sureJyqc = this.data.sureJyqc
        var status_id = 1
        if (sureJyqc) {
            status_id = 2
        }
        this.setData({
            sureJyqc: !sureJyqc
        })
        this.addDelJyqc({ user_id: wx.getStorageSync("user_id"), jyqc_id: this.data.chefId, status_id })
    },
    // 切换分类
    changeIndex(e) {
        var current = e.currentTarget.dataset.idx
        var id = e.currentTarget.dataset.id
        this.setData({
            current,
            page: 1,
            over: false,
            show: false,
            scrollId: "I" + current
        })
        if (current == -1) { //判断是否为全部按钮(是)
            this.getAllCookBooks({ id: this.data.chefId, page: 1, size: this.data.size }, 0)
        } else { //判断是否为全部按钮(否)
            this.getCookbooksById({ id: this.data.chefId, page: 1, size: this.data.size, cid: id }, 0)
        }
    },
    // 计数
    count() {
        var newList = wx.getStorageSync("select-menus") || []
        newList.filter((item) => {
            return item.num
        })
        var count = 0
        newList.map((item) => {
            count += item.num
        })
        this.setData({
            num: count
        })
    },
    close() {
        this.setData({
            show: false
        })
    },
    toMain() {
        if (this.data.num == 0) {
            wx.showToast({
                title: '您还未选菜',
                icon: "none"
            })
        } else {
            wx.navigateTo({
                url: "/pages/mainCourse/mainCourse"
            })
        }
    },
    // 点击添加菜谱
    add(e) {
        var that = this
        var listMenus = wx.getStorageSync('select-menus') || [];
        for (let v of listMenus) {
            if (v.id == e.target.dataset.id) {
                wx.showToast({
                    title: '请不要重复添加',
                    icon: 'none',
                    duration: 1500
                })
                return false;
            }
        }
        listMenus.push({
            flag: true,
            id: e.target.dataset.id,
            name: e.target.dataset.name,
            main_img_url: e.target.dataset.img,
            ingredients: [],
            count: e.target.dataset.count,
            time: e.target.dataset.time,
            seasoning_count: e.target.dataset.seasoning_count,
            num: 1
        });
        wx.setStorageSync("select-menus", listMenus);
        wx.showToast({
            title: '添加成功',
            icon: 'success',
            duration: 1500
        })
        this.count()
    },

    toMain(e) {
        if (this.data.num == 0) {
            wx.showToast({
                title: '您还未选菜',
                icon: "none"
            })
        } else {
            wx.navigateTo({
                url: "/pages/mainCourse/mainCourse"
            })
        }
    },
    // 去菜谱详情页面
    toVideo(e) {
        var id = e.target.dataset.id
        wx.navigateTo({
            url: "/pages/video/video?id=" + id
        })
    },
    // 点击展开更多分类
    more() {
        this.setData({
            show: true
        })
    },
    // 点击关闭更多分类
    close() {
        this.setData({
            show: false
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var chefId = options.id || 1
        this.setData({
            chefId
        })
        this.count()
        this.getOneJyqc({ id: chefId })
        this.sureJyqc({ user_id: wx.getStorageSync("user_id"), jyqc_id: chefId })
        this.getAllCookBooks({ id: this.data.chefId, page: 1, size: this.data.size }, 0)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        var page = this.data.page + 1
        this.setData({
            page
        })
        if (this.data.current == -1) { //判断是否为全部按钮(是)
            this.getAllCookBooks({ id: this.data.chefId, page, size: this.data.size }, 1)
        } else { //判断是否为全部按钮(否)
            this.getCookbooksById({ id: this.data.chefId, page, size: this.data.size, cid: id }, 1)
        }
    }
})